package com.jz.community.moduleshoppingguide.nearshop.ui.view;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.jz.community.basecomm.utils.RxDataTool;
import com.jz.community.basecomm.utils.WpToast;
import com.jz.community.moduleshoppingguide.R;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;

public class StairDialogBuilder extends QMUIDialog.AutoResizeDialogBuilder {

    private Context mContext;
    private EditText mEditText;
    private int goods_num;
    private String gid;

    public StairDialogBuilder(Context context) {
        super(context);
        mContext = context;
    }

    public StairDialogBuilder(Context context, int goods_num, String gid) {
        super(context);
        mContext = context;
        this.goods_num = goods_num;
        this.gid = gid;
    }

    public EditText getEditText() {
        return mEditText;
    }

    @Override
    public View onBuildContent(final QMUIDialog dialog, ScrollView parent) {
        View rootView = LayoutInflater.from(mContext).inflate(R.layout.module_shoppingguide_dialog_stair_cart, null);
        //dismiss
        LinearLayout back = rootView.findViewById(R.id.dialog_stair_back);
        //减
        ImageView iv_sub = rootView.findViewById(R.id.dialog_stair_edit_sub);
        //加
        ImageView iv_add = rootView.findViewById(R.id.dialog_stair_edit_add);
        //输入框
        mEditText = rootView.findViewById(R.id.dialog_stair_edit);
        //确定
        TextView enter = rootView.findViewById(R.id.dialog_stair_edit_enter);
        String content = goods_num + "";
        mEditText.setText(content);
        mEditText.setSelection(content.length());
        back.setOnClickListener(v -> dialog.dismiss());
        iv_sub.setOnClickListener(v -> {
            if (goods_num > 0) {
                goods_num--;
                mEditText.setText(goods_num + "");
            } else {
                WpToast.l(mContext, "已是最小数量了");
                mEditText.setText(0 + "");
            }

        });

        iv_add.setOnClickListener(v -> {
            if (goods_num >= 999) {
                WpToast.l(mContext, "已是最大数量了");
                mEditText.setText(999 + "");
            } else {
                goods_num++;
                mEditText.setText(goods_num + "");
            }
        });

        enter.setOnClickListener(v -> {
            if (!RxDataTool.isNullString(mEditText.getText().toString().trim())) {
                int change_num = Integer.parseInt(mEditText.getText().toString().trim());
                if (change_num >= 0) {
                    editNumb.currentNumb(change_num);
                    dialog.dismiss();
                }
            } else {
                WpToast.l(mContext, "请输入正确数量!");
            }
        });
        return rootView;
    }

    IEditNumb editNumb;

    public interface IEditNumb {
        void currentNumb(int numb);
    }

    public void setEditNumb(IEditNumb editNumb) {
        this.editNumb = editNumb;
    }
}
